package com.franzliszt.maillist.atcivity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.franzliszt.maillist.MainActivity;
import com.franzliszt.maillist.R;
import com.franzliszt.maillist.sql.Dao;
import com.franzliszt.maillist.sql.News;
import com.franzliszt.maillist.util.KillProcess;

import org.w3c.dom.Node;

import java.util.ArrayList;
import java.util.List;
import java.util.prefs.NodeChangeEvent;

public class DetailsActivity extends AppCompatActivity {
    private LinearLayout Call,DeleteLayout;
    private TextView EditMessage,LastName,Name;
    private Button DeleteContact;
    private ImageView Modify1,Modify2,Modify3,Modify4;
    private EditText Phone,Address,School,Note;
    private Dao dao;
    private String PhoneNumber;
    private List<News> newsList = new ArrayList<>();
    private int count = 1;
    private KillProcess killProcess;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE );
            getWindow().setStatusBarColor( Color.TRANSPARENT );
        }
        setContentView(R.layout.activity_details);
        InitView();
        UpdateView();
        DeleteContact.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dao.Delete(PhoneNumber);
                startActivity(new Intent(DetailsActivity.this,ContactsActivity.class));
            }
        });

        Call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + PhoneNumber));
                if (ActivityCompat.checkSelfPermission(DetailsActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(DetailsActivity.this, new String[]{Manifest.permission.CALL_PHONE}, 1);
                    Toast.makeText(DetailsActivity.this,"未申请拨号权限",Toast.LENGTH_LONG).show();
                    startActivity(intent);
                    return;
                }
                startActivity(intent);
            }
        });
    }
    private void InitView(){
        Call = findViewById(R.id.Call);
        EditMessage = findViewById(R.id.AddContacts);
        EditMessage.setText("编辑");
        DeleteLayout = findViewById(R.id.DeleteLayout);
        DeleteContact = findViewById(R.id.DeleteContact);
        Phone = findViewById(R.id.D_PhoneNumber);
        Address = findViewById(R.id.D_Address);
        School = findViewById(R.id.D_School);
        Note = findViewById(R.id.D_Note);
        LastName = findViewById(R.id.lastName);
        Name = findViewById(R.id.Name);
        Modify1 = findViewById(R.id.Modify_1);
        Modify2 = findViewById(R.id.Modify_2);
        Modify3 = findViewById(R.id.Modify_3);
        Modify4 = findViewById(R.id.Modify_4);

        killProcess = KillProcess.getInstance();
        killProcess.PushActivity(DetailsActivity.this);
        dao = new Dao(DetailsActivity.this);
        PhoneNumber = getIntent().getStringExtra("PhoneNumber");
        if (!TextUtils.isEmpty(PhoneNumber)){
            Phone.setText(PhoneNumber);
        }
    }
    private void UpdateView(){
        try{
            newsList = dao.QueryAll();
        }catch (NullPointerException e){
            e.printStackTrace();
        }
        if (newsList != null){
            for (int i = 0; i <newsList.size() ; i++) {
                if (PhoneNumber.equals(newsList.get(i).getPhoneNumber())){
                    Address.setText(newsList.get(i).getAddress());
                    School.setText(newsList.get(i).getSchool());
                    Note.setText(newsList.get(i).getOther());
                    LastName.setText(newsList.get(i).getLastName().substring(0,1));
                    Name.setText(newsList.get(i).getLastName()+newsList.get(i).getFirstName());
                }
            }
        }
    }

    public void OtherThings(View view){
        count++;
        if (count %2 == 0){
            //编辑状态
            UnLock();
            EditMessage.setText("完成");
        }else {
            //正常状态
            Lock();
            UpdateSQL();
            EditMessage.setText("编辑");
        }
    }
    public void Exit(View view){
        startActivity(new Intent(DetailsActivity.this,ContactsActivity.class));
    }
    private void UpdateSQL(){
        String addr = Address.getText().toString();
        String school = School.getText().toString();
        String node = Note.getText().toString();
        dao.Update(PhoneNumber,school,addr,node);
    }
    //正常状态
    private void Lock(){
        Call.setEnabled(true);
        Call.setClickable(true);

//        Phone.setEnabled(false);
//        Phone.setClickable(false);

        Address.setEnabled(false);
        Address.setClickable(false);

        School.setEnabled(false);
        School.setClickable(false);

        Note.setEnabled(false);
        Note.setClickable(false);
        DeleteLayout.setVisibility(View.GONE);

        Modify1.setVisibility(View.GONE);
        Modify2.setVisibility(View.GONE);
        Modify3.setVisibility(View.GONE);
        Modify4.setVisibility(View.GONE);
    }
    //编辑状态
    private void UnLock(){
        Call.setEnabled(false);
        Call.setClickable(false);

//        Phone.setEnabled(true);
//        Phone.setClickable(true);

        Address.setEnabled(true);
        Address.setClickable(true);

        School.setEnabled(true);
        School.setClickable(true);

        Note.setEnabled(true);
        Note.setClickable(true);
        DeleteLayout.setVisibility(View.VISIBLE);

        Modify1.setVisibility(View.VISIBLE);
        Modify2.setVisibility(View.VISIBLE);
        Modify3.setVisibility(View.VISIBLE);
        Modify4.setVisibility(View.VISIBLE);
    }
}
